package LC;

import java.util.LinkedList;

/**
 * @Classname LC1609
 * @Description // https://leetcode-cn.com/problems/even-odd-tree/ 树 一次ac
 * @Date 2021/4/21 16:16
 * @Created by yin
 */
public class LC1609 {
    public boolean isEvenOddTree(TreeNode root) {
        if (root == null) return true;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            int t1 = Integer.MAX_VALUE, t2 = Integer.MIN_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                int val = node.val;
                if (level % 2 == 0) {
                    // 奇数
                    if (val % 2 == 0) {
                        return false;
                    }
                    // 递增
                    if (val <= t2) {
                        return false;
                    }
                    t2 = val;
                } else {
                    // 偶数
                    if (val % 2 != 0) {
                        return false;
                    }
                    // 递减少
                    if (val >= t1) {
                        return false;
                    }
                    t1 = val;
                }
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            level++;
        }
        return true;
    }
}
